package cn.zn.modules.budget.cur.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.zn.framework.common.exception.RRException;
import cn.zn.framework.common.utils.FileUtils;
import cn.zn.modules.budget.cur.dao.TBudgetInfoDetailAttachMapper;
import cn.zn.modules.budget.cur.entity.TBudgetInfoDetailAttach;
import cn.zn.modules.budget.cur.service.TBudgetInfoDetailAttachService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.Collection;
import java.util.List;


@Service("tBudgetInfoDetailAttachService")
public class TBudgetInfoDetailAttachServiceImpl extends ServiceImpl<TBudgetInfoDetailAttachMapper, TBudgetInfoDetailAttach> implements TBudgetInfoDetailAttachService {
    @Autowired
    private FileUtils fileUtils;

    @Override
    public void saveAttach(String detailId, MultipartFile[] fileupload) {
        try {
            LocalDate now = LocalDate.now();
            Collection<TBudgetInfoDetailAttach> attaches = CollUtil.newArrayList();
            String fileName = "";
            TBudgetInfoDetailAttach tBudgetInfoDetailAttach;
            File outputFile;
            for (MultipartFile multipartFile : fileupload) {
                fileName = multipartFile.getOriginalFilename();
                outputFile = fileUtils.updateFile(multipartFile.getInputStream(), fileName.substring(fileName.lastIndexOf(".") + 1), now.toString().split("-"));
                tBudgetInfoDetailAttach = new TBudgetInfoDetailAttach();
                tBudgetInfoDetailAttach.setTBudgetInfoDetailId(detailId);
                tBudgetInfoDetailAttach.setContentType(multipartFile.getContentType());
                tBudgetInfoDetailAttach.setGenerateName(outputFile.getName().substring(0, outputFile.getName().lastIndexOf(".")));
                tBudgetInfoDetailAttach.setPathLocal(outputFile.getPath());
                tBudgetInfoDetailAttach.setPathRelative(fileUtils.getNoBasePath(outputFile.getPath()).replace(File.separator, "/"));
                tBudgetInfoDetailAttach.setRealName(fileName.substring(0, fileName.lastIndexOf(".")));
                tBudgetInfoDetailAttach.setSize(multipartFile.getSize());
                tBudgetInfoDetailAttach.setSuffix(fileName.substring(fileName.lastIndexOf(".") + 1));
                tBudgetInfoDetailAttach.setIsMain(false);
                attaches.add(tBudgetInfoDetailAttach);
            }
            super.saveBatch(attaches);
        } catch (IOException e) {
            log.error("", e);
            throw new RRException("");
        }
    }

    @Override
    public void deleteAttach(List<String> ids) {
        List<TBudgetInfoDetailAttach> attaches = baseMapper.selectBatchIds(ids);
        attaches.forEach(o -> {
            FileUtil.del(o.getPathLocal());
            baseMapper.deleteById(o.getId());
        });
    }
}
